package Top200;

/**
 * @author zhangmin
 * @create 2022-02-21 15:27
 * 二分查找
 */
public class search33 {
    public int search(int[] nums, int target) {
        int left=0,right=nums.length-1;
        while (left<=right){
            int mid=left+(right-left)/2;
            if (nums[mid]==target){
                return mid;
            }else if (nums[mid]<=nums[right]){
                //此时mid后半部分为升序,旋转点再左半部分
                if (nums[mid]<target&&target<=nums[right]){
                    left=mid+1;
                }else {
                    right=mid-1;
                }
            }else {
                //此时mid前面的部分为升序的，旋转点再后半部分
                if (nums[left]<=target&&target<nums[mid]){
                    right=mid-1;
                }else {
                    left=mid+1;
                }
            }
        }
        return nums[left]==target?left:-1;
    }
}
